// pages/register/register.js
var alert = require("../../utils/alert.js");
var servicePath = require("../../utils/servicePath.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isAgree: true, //是否同意
    regInfo: {
      openId: null,
      phone: null,
      phoneCode: null //用户输入的验证码
    },
    phoneCode: "", //验证码
    sended:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindAgreeChange: function () {
    var tmp = this.data.isAgree
    this.setData({
      isAgree: !tmp
    })
  },
  phoneInput: function (e) {
    var tmp = this.data.regInfo
    this.setData({
      regInfo: {
        openId: tmp.openId,
        phone: e.detail.value,
        phoneCode: tmp.phoneCode //用户输入的验证码
      }
    })
  },
  phoneCodeInput: function (e) {
    var tmp = this.data.regInfo
    this.setData({
      regInfo: {
        openId: tmp.openId,
        phone: tmp.phone,
        phoneCode: e.detail.value //用户输入的验证码
      }
    })
  },
  //点击立即注册
  registerNow: function () {
    var tmp = this.data.regInfo;
    var openId = getApp().globalData.openId;

    if (openId == null) {
      alert.toast("未获取到信息");
      return;
    } else {
      tmp.openId = openId;
    }
    if (tmp.phone == null) {
      alert.toast("请输入手机号");
      return;
    }
    if (tmp.phoneCode == "" || tmp.phoneCode == null || tmp.phoneCode != this.data.phoneCode) {
      alert.toast("验证码不正确");
      return;
    }
    let reqPath = servicePath.RegisterService();
    console.log(tmp.openId);
    wx.request({
      url: reqPath,
      data: {
        tel: tmp.phone,
        weixin: tmp.openId
      },

      method: "POST",
      success: function (res) {
        console.log(res);
        if (res.data.code == 200 || res.data.code == 300) {
          getApp().globalData.backUserInfo = res.data.condition;
          alert.toast("登录成功");
          wx.switchTab({
            url: '/pages/my/my'
          })
        }
      }
    })

  },
  backView: function () {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  getCode: function () {
    var isSend = this.data.sended;
    if(isSend){
      alert.toast("验证码已发送，请勿重新发送");
      return;
    }
    var apiUrl = getApp().globalData.apiDomain;
    var tmp = this.data.regInfo;
    if (tmp.phone == null) {
      alert.toast("请输入手机号");
      return;
    }
    var _this = this;
    wx.request({
      url: apiUrl + "/Message/Send?tel=" + tmp.phone,
      method: "GET",
      success: function (res) {
        alert.toast("验证码已发送");
        if (res.data.code == 200) {
          _this.setData({
            phoneCode: res.data.msg,
            sended:true
          })

        }
      }
    })
  }
})